using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.NEWS.Controls
{
    public partial class Comment : ModulePage
    {
        protected override void OnInit(EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                txtEmail.Text = PortalSecurity.CurrentUser.Identity.Email;
            }
            pnlComment.Visible = Request.IsAuthenticated;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hlSignIn.NavigateUrl = "~/SignIn.aspx?ReturnUrl=" + Server.UrlEncode(Request.RawUrl);
                if (Context.User.Identity.IsAuthenticated)
                {
                    pnlSignIn.Visible = false;
                }
                else
                {
                    pnlSignIn.Visible = true;
                }

                if (this.ItemID > 0)
                {
                    dlComments.DataSource = Components.Comment.SelectBy_ItemID(this.ItemID);
                    dlComments.DataBind();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Comment comment = new Components.Comment();
                comment.ItemID = Convert.ToInt32(Request.QueryString["itemID"]);
                comment.Content = Server.HtmlEncode(txtContent.Text);
                comment.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                comment.CreationTime = DateTime.Now;
                comment.Insert();

                txtContent.Text = string.Empty;
                dlComments.DataSource = Components.Comment.SelectBy_ItemID(this.ItemID);
                dlComments.DataBind();
            }
        }
    }
}